package edu.guigu.appquickstart.datastore.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.datastore.bean.UserBean;

/**
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/12 21:38
 */
public class UserBeanAdapter extends RecyclerView.Adapter<UserBeanAdapter.UBViewHolder> {

    private Context context;
    private List<UserBean> datas;
    public UserBeanAdapter(Context context,List<UserBean> datas){
        this.context = context;
        this.datas = datas;
    }
    @NonNull
    @Override
    public UBViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_list_db_userbean,parent,false);
        return new UBViewHolder(itemView);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onBindViewHolder(@NonNull UBViewHolder holder, int position) {
        UserBean userBean = datas.get(position);
        holder.tv_name.setText( userBean.getName() );
        holder.tv_age.setText( String.valueOf(userBean.getAge()) );
        holder.tv_index.setText( String.valueOf(userBean.getId()) );
    }
    // 当前长按位置
    public int cLongPosition;

    class UBViewHolder extends RecyclerView.ViewHolder{
        TextView tv_index,tv_name,tv_age;
        public UBViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_index = itemView.findViewById(R.id.tv_index);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_age = itemView.findViewById(R.id.tv_age);

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    cLongPosition = getAdapterPosition();
                    return false;
                }
            });
        }
    }
}
